package com.example.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exam.dashboard.IndexVM;
import com.example.exam.mapper.*;
import com.example.exam.pojo.entity.TExamPaperAnswer;
import com.example.exam.pojo.entity.TExamPaperQuestionCustomerAnswer;
import com.example.exam.pojo.entity.TQuestion;
import com.example.exam.pojo.entity.TUser;
import com.example.exam.tools.JsonResult;
import io.swagger.models.auth.In;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LENOVO
 * @description TODO
 * @date 2023/4/4 1:21
 */

@RestController("AdminDashboardController")
@RequestMapping(value = "/api/admin/dashboard")
public class Dashboard {

    @Autowired
    private TExamPaperMapper tExamPaperMapper;
    @Autowired
    private TQuestionMapper tQuestionMapper;
    @Autowired
  private TExamPaperAnswerMapper tExamPaperAnswerMapper;
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TExamPaperQuestionCustomerAnswerMapper tExamPaperQuestionCustomerAnswerMapper;

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public JsonResult<IndexVM> Index() {
        IndexVM vm = new IndexVM();
        Long examPaperCount = tExamPaperMapper.selectCount(null);
        Long questionCount = tQuestionMapper.selectCount(null);
        Long doExamPaperCount = tExamPaperQuestionCustomerAnswerMapper.selectCount(null);
        Long doQuestionCount =tExamPaperAnswerMapper.selectCount(null) ;

        vm.setExamPaperCount(Math.toIntExact(examPaperCount));
        vm.setQuestionCount(Math.toIntExact(questionCount));
        vm.setDoExamPaperCount(Math.toIntExact(doQuestionCount));
        vm.setDoQuestionCount(Math.toIntExact(doExamPaperCount));


        List<String> sdfList = new ArrayList<>();
        Map<String, Integer>map1=new TreeMap<>();
        Map<String, Integer>map2=new TreeMap<>();

        for (int i = 0; i < 7; i++) {
            Date date = new Date(System.currentTimeMillis() - i * 24 * 60 * 60 * 1000L);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            map1.put(format,0);
            map2.put(format,0);
            sdfList.add(format);
        }
        Collections.reverse(sdfList);
        vm.setMothDayText(sdfList);


        List<Integer> mothDayUserActionValue =new LinkedList<>();
        List<Map<String, Object>> userList = tUserMapper.selectMaps(new QueryWrapper<TUser>()
                .select("date_format(create_time, '%Y-%m-%d') as create_date")// 将datetime类型的字段转换为日期格式
                .groupBy("create_date").orderByDesc("create_date")); ;// 按照日期进行每一组数据，获取当天的用户数量
        for (Map<String, Object> map : userList) {
            String createDate = (String) map.get("create_date");// 获取日期
            Long count = tUserMapper.selectCount(new LambdaQueryWrapper<TUser>()
                    .apply("date_format(create_time, '%Y-%m-%d') = '" + createDate + "'"));// 获取当天的用户数量
           // System.out.println("日期：" + createDate + "，用户数量：" + count);
            if(map1.containsKey(createDate)){
                map1.put(createDate, Math.toIntExact(count));
            }
        }


        vm.setMothDayUserActionValue(new ArrayList<>(map1.values()));

        List<Integer>examQuestionValue =new LinkedList<>();
        List<Map<String, Object>> questionList = tQuestionMapper.selectMaps(new QueryWrapper<TQuestion>()
                .select("date_format(create_time, '%Y-%m-%d') as create_date")// 将datetime类型的字段转换为日期格式
                .groupBy("create_date").orderByDesc("create_date"));// 按照日期进行每一组数据，获取当天的用户数量
        for (Map<String, Object> map : questionList) {
            String createDate = (String) map.get("create_date");// 获取日期
            Long count1 = tQuestionMapper.selectCount(new LambdaQueryWrapper<TQuestion>()
                    .apply("date_format(create_time, '%Y-%m-%d') = '" + createDate + "'"));// 获取当天的用户数量
            // System.out.println("日期：" + createDate + "，用户数量：" + count);
            examQuestionValue.add(Math.toIntExact(count1));
            if(map2.containsKey(createDate)){
                map2.put(createDate, Math.toIntExact(count1));
            }
        }

        vm.setMothDayDoExamQuestionValue(new ArrayList<>(map2.values()));



        return JsonResult.ok(vm);
    }
}
